/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.animation.interaction.progressanimation;

import com.bytedance.scene.animation.interaction.evaluator.IntEvaluator;
import ohos.agp.components.Image;
import ohos.utils.ObjectAttribute;

import java.util.HashMap;
import java.util.Set;

public class ImageViewAnimationBuilder extends ViewOtherAnimationBuilder<ImageViewAnimationBuilder> {
    private static final ObjectAttribute<Image, Float> IMAGE_ALPHA = new ObjectAttribute<Image, Float>(Float.class, "alpha") {
        @Override
        public void set(Image object, Float value) {
            object.setAlpha(value);
        }

        @Override
        public Float get(Image object) {
            return object.getAlpha();
        }
    };

    private Image mView;
    private HashMap<ObjectAttribute, Holder> hashMap = new HashMap<>();

    ImageViewAnimationBuilder(Image view) {
        super(view);
        this.mView = view;
    }

    public ImageViewAnimationBuilder imageAlpha(float fromValue, float toValue) {
        hashMap.put(IMAGE_ALPHA, new Holder(new IntEvaluator(), fromValue, toValue));
        return this;
    }

    public ImageViewAnimationBuilder imageAlphaBy(int deltaValue) {
        return imageAlpha(mView.getAlpha() + deltaValue);
    }

    public ImageViewAnimationBuilder imageAlpha(float value) {
        return imageAlpha(mView.getAlpha(), value);
    }

    @Override
    protected void onProgress(float progress) {
        super.onProgress(progress);

        Set<ObjectAttribute> set = hashMap.keySet();
        for (ObjectAttribute property : set) {
            Holder value = hashMap.get(property);
            property.set(mView, value.typeEvaluator.evaluate(progress, value.fromValue, value.toValue));
        }
    }
}
